import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "../pages/Layout";
import ProductSelect from "../pages/ProductSelect/ProductSelect";
import Params from "../pages/Params/Params";
import Content from "../pages/Content/Content";
import Login from "../pages/Login/Login";
import store from "../store";

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    // name: 'Layout',
    component: Layout,
    children:[
      {
        path:'/',
        name:'ProductSelect',
        component:ProductSelect,
        meta:{
          isLogin:true
        }
      },{
        path:'/params',
        name:'Params',
        component:Params,
        meta:{
          isLogin:true
        }
      },{
        path:'/content',
        name:'Content',
        component:Content,
        meta:{
          isLogin:true
        }
      }
    ]
  },
  {
    path:'/login',
    name:'Login',
    component:Login
  }
];
const router = new VueRouter({
  linkActiveClass:"active",
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});


router.beforeEach((to, from, next) => {
  if (to.meta.isLogin){
    let flag = store.state.egoVuex.tokin;
    if (flag){
      next();
    }else {
      next({
        path:'/login'
      })
    }
  }else {
    // 不需要登录
    next();
  }
});


export default router
